﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LOMBARDISPICK.Web.Helpers;
using LOMBARDISPICK.Model;
using LOMBARDISPICK.Domain.Services;

namespace LOMBARDISPICK.Web.Controllers
{
	public class CalculateOddsController : Controller
	{
		private readonly IPredictionService predictionService;
		private readonly INFLTeamsService nflTeamsService;
		private readonly ITeamSeason_1985_2011Service teamSeasonService;

		public CalculateOddsController(
				INFLTeamsService nflTeamsService,
				IPredictionService predictionService,
				ITeamSeason_1985_2011Service teamSeasonService
			)
		{
			this.nflTeamsService = nflTeamsService;
			this.predictionService = predictionService;
			this.teamSeasonService = teamSeasonService;
		}

		private void ViewDataSelectLists()
		{
			ViewData["NFLTeamsID"] = this.nflTeamsService.ViewDataSelectLists();
			ViewData["YearToCalculate"] = getYears();
		}

		public ActionResult GetImage(int id)
		{
			byte[] byteArray = this.nflTeamsService.getById(id).Helmet;                
			if (byteArray != null)
				return File(byteArray, "image/jpeg");
			return null;
		}

		public IEnumerable<SelectListItem> getYears()
		{
			//HTML3.2, HTML4.0, MHTML, IMAGE, EXCEL, WORD, CSV, PDF, XML, and NULL
			return new[]
			{
				new SelectListItem { Value = "2012", Text = "2012" },
				new SelectListItem { Value = "2001", Text = "2001" },
				new SelectListItem { Value = "2002", Text = "2002" },
				new SelectListItem { Value = "2003", Text = "2003" },
				new SelectListItem { Value = "2004", Text = "2004" },
				new SelectListItem { Value = "2005", Text = "2005" },
				new SelectListItem { Value = "2006", Text = "2006" },
				new SelectListItem { Value = "2007", Text = "2007" },
				new SelectListItem { Value = "2008", Text = "2008" },
				new SelectListItem { Value = "2009", Text = "2009" },
				new SelectListItem { Value = "2010", Text = "2010" },
				new SelectListItem { Value = "2011", Text = "2011" }
			};
		}

		//[AllowAnonymous]
		public ActionResult Index()
		{
			var teamSeasonStats = this.teamSeasonService.getMany(x => x.NFLTeam.TeamNameAbbreviations == "GB" && x.Season == 2011).SingleOrDefault();

			LPCoefficient model = new LPCoefficient();
			model.NFLTeamsID = teamSeasonStats.NFLTeamsID;
			model.PasserRating = teamSeasonStats.PasserRating;
			model.PointsPerGameOffense = teamSeasonStats.Season /16;
			model.PointsPerGameDefense = teamSeasonStats.OpponentScore /16;
			model.TurnoverDifferential = teamSeasonStats.TurnoverDifferential;
			model.redzonePercentageOffense = (decimal)teamSeasonStats.RedZoneConversionPercentage;
			model.redZonePercentageDefense = (decimal)teamSeasonStats.OpponentRedZoneConversionsPercentage;

			model.TimeOfPosession = teamSeasonStats.TimeOfPosession;
			model.PassingYardsPerAttempt = teamSeasonStats.PassingYardsPerAttempt;
			model.ThirdDownPercentage = teamSeasonStats.ThirdDownPercentage;
			model.FourthDownPercentage = teamSeasonStats.FourthDownPercentage;
			model.Penalties = teamSeasonStats.Penalties;
			model.PenaltyYards = teamSeasonStats.PenaltyYards;
			model.TimesSacked = teamSeasonStats.TimesSacked;
			model.TimesSackedYards = teamSeasonStats.TimesSackedYards;
			model.QuarterbackHits = teamSeasonStats.QuarterbackHits;
			model.OpponentTimeOfPosession = teamSeasonStats.OpponentTimeOfPosession;
			model.OpponentPassingYardsPerAttempt = teamSeasonStats.OpponentPassingYardsPerAttempt;
			model.OpponentPasserRating = teamSeasonStats.OpponentPasserRating;
			model.OpponentThirdDownPercentage = teamSeasonStats.OpponentThirdDownPercentage;
			model.OpponentFourthDownPercentage = teamSeasonStats.OpponentFourthDownPercentage;
			model.OpponentPenalties = teamSeasonStats.OpponentPenalties;
			model.OpponentPenaltyYards = teamSeasonStats.OpponentPenaltyYards;
			model.OpponentTimesSacked = teamSeasonStats.OpponentTimesSacked;
			model.OpponentTimesSackedYards = teamSeasonStats.OpponentTimesSackedYards;
			model.Punts = teamSeasonStats.Punts;
			model.OpponentPunts = teamSeasonStats.OpponentPunts;
			model.OpponentTurnoverDifferential = teamSeasonStats.OpponentTurnoverDifferential;

			ViewDataSelectLists();
			return View();
		}

		//[AllowAnonymous]
		public ActionResult QuickPicks()
		{
			ViewDataSelectLists();
			return View();
		}

		public ActionResult Calculate(LPCoefficient data)
		{
			//this.predictionService.CalculateWeka(2011, data);
			//ViewBag.RandomPercent = this.predictionService.LPCoefficient(data);
			//ViewBag.RandomPercent = F();

			List<LPWekaPrediction> LPWekaPredictionModel = this.predictionService.CalculateWeka(data.YearToCalculate, data);
			ViewData["winTotal"] = LPWekaPredictionModel.Select(x => x.WinPrediction).Sum();
			ViewData["runnerUpTotal"] = LPWekaPredictionModel.Select(x => x.RunnerUpPrediction).Sum();
			return View(LPWekaPredictionModel);
		}

		public ActionResult CalculateQuickPicks(FormCollection form)
		{
			int YearToCalculate = int.Parse(form.Get("YearToCalculate"));
			int YearForLinq = YearToCalculate;
			if (YearForLinq == 2012)
			{
				YearForLinq = 2011;
			}

			var teamSeasonStats = this.teamSeasonService.getMany(x => x.NFLTeam.TeamNameAbbreviations == "GB" && x.Season == YearForLinq).SingleOrDefault();

			LPCoefficient model = new LPCoefficient();
			model.NFLTeamsID = teamSeasonStats.NFLTeamsID;
			model.YearToCalculate = YearToCalculate;
			model.PasserRating = teamSeasonStats.PasserRating;
			model.PointsPerGameOffense = teamSeasonStats.Season / 16;
			model.PointsPerGameDefense = teamSeasonStats.OpponentScore / 16;
			model.TurnoverDifferential = teamSeasonStats.TurnoverDifferential;
			model.redzonePercentageOffense = (decimal)teamSeasonStats.RedZoneConversionPercentage;
			model.redZonePercentageDefense = (decimal)teamSeasonStats.OpponentRedZoneConversionsPercentage;

			model.TimeOfPosession = teamSeasonStats.TimeOfPosession;
			model.PassingYardsPerAttempt = teamSeasonStats.PassingYardsPerAttempt;
			model.ThirdDownPercentage = teamSeasonStats.ThirdDownPercentage;
			model.FourthDownPercentage = teamSeasonStats.FourthDownPercentage;
			model.Penalties = teamSeasonStats.Penalties;
			model.PenaltyYards = teamSeasonStats.PenaltyYards;
			model.TimesSacked = teamSeasonStats.TimesSacked;
			model.TimesSackedYards = teamSeasonStats.TimesSackedYards;
			model.QuarterbackHits = teamSeasonStats.QuarterbackHits;
			model.OpponentTimeOfPosession = teamSeasonStats.OpponentTimeOfPosession;
			model.OpponentPassingYardsPerAttempt = teamSeasonStats.OpponentPassingYardsPerAttempt;
			model.OpponentPasserRating = teamSeasonStats.OpponentPasserRating;
			model.OpponentThirdDownPercentage = teamSeasonStats.OpponentThirdDownPercentage;
			model.OpponentFourthDownPercentage = teamSeasonStats.OpponentFourthDownPercentage;
			model.OpponentPenalties = teamSeasonStats.OpponentPenalties;
			model.OpponentPenaltyYards = teamSeasonStats.OpponentPenaltyYards;
			model.OpponentTimesSacked = teamSeasonStats.OpponentTimesSacked;
			model.OpponentTimesSackedYards = teamSeasonStats.OpponentTimesSackedYards;
			model.Punts = teamSeasonStats.Punts;
			model.OpponentPunts = teamSeasonStats.OpponentPunts;
			model.OpponentTurnoverDifferential = teamSeasonStats.OpponentTurnoverDifferential;

			List<LPWekaPrediction> LPWekaPredictionModel = this.predictionService.CalculateWeka(YearToCalculate, model);

			LPWekaPredictionModel = LPWekaPredictionModel.OrderByDescending(x => x.WinPrediction).ThenByDescending(x => x.RunnerUpPrediction).ThenByDescending(x => x.NoPrediction).ToList();
			ViewData["winTotal"] = LPWekaPredictionModel.Select(x => x.WinPrediction).Sum();
			ViewData["runnerUpTotal"] = LPWekaPredictionModel.Select(x => x.RunnerUpPrediction).Sum();
			return View(LPWekaPredictionModel);
		}

		static Random _r = new Random();
		static int F()
		{
			// Use class-level Random so that when this
			// ... method is called many times, it still has
			// ... good Randoms.
			int n = _r.Next(101);
			// If this declared a local Random, it would
			// ... repeat itself.
			return n;
		}

		//public ActionResult wekaPrediction()
		//{
		//    //this.predictionService.testWeka(2011)
		//    return View();
		//}
	}
}